package com.wmlt.service.impl;

import com.wmlt.dao.BaseDao;
import com.wmlt.dao.MessageDao;
import com.wmlt.dao.impl.MessageDaoImpl;
import com.wmlt.dao.impl.UserDaoImpl;
import com.wmlt.entity.Message;
import com.wmlt.service.MessageService;
import com.wmlt.util.JdbcUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class MessageServiceImpl implements MessageService {
    MessageDao messageDao = new MessageDaoImpl();
    int result = 0;
    Connection connection=null;
    @Override
    public int sendMessage(Message message) {
        connection = JdbcUtil.getConnection();
        try {
            connection.setAutoCommit(false);//设置手动提交事物
            result = messageDao.sendMessage(message);
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        } finally {
            JdbcUtil.close(null, null, connection);
        }
        return result;
    }

    @Override
    public List<Message> getMessageListByUserId(String senderUserId,String addresseeUserId) {
        return messageDao.getMessageListByUserId(senderUserId,addresseeUserId);
    }

    @Override
    public List<Message> getMessageRecord(String senderUserId, String addresseeUserId) {
        return messageDao.getMessageRecord(senderUserId, addresseeUserId);
    }
}